﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using CosyTrade.ClientForm.Printer;
using CosyTrade.Organization;
using CosyTrade.Store;
using CosyTrade.Trade;
using CosyTrade.UI;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using CosyTrade.ClientForm.Properties;

namespace CosyTrade.ClientForm.Trade
{
    public partial class Import_Countting : FormBase
    {
        private Image headimage;
        private Image bodyimage;
        private Font captionFont;
        private Color captionColor;
        private bool paintCaption;

        public Import_Countting()
        {
            InitializeComponent();
        }

        protected override void InitCommandBar()
        {
            ToolbarItems.AddCommandItem(new ToolbarCommandItem("ImportBillCounttingOK",Resources.accept, "点货完毕", true, true, true,
                                                               ComfirmCounttingCallback));

            ToolbarItems.AddCommandItem(new ToolbarCommandItem("ImportBillDetail", Resources.table, "进口单明细", true, true, true,
                                                               BillDetailCallback));

            ToolbarItems.AddCommandItem("PrintImportBill", Resources.printer, "打印进口单", true, true, true, PrintImportBillCallback);

            ToolbarItems.AddCommandItem(new ToolbarCommandItem("headAndbodySetting2", Resources.map, "相簿封面与背景", true, true, true,
                                                               TemplateSettingCallBack));
            ToolbarItems.AddCommandItem(new ToolbarCommandItem("PrintCategoryBook2", Resources.printer, "打印相簿", true, true, true,
                                                               PrintCategoryBookCallback));


            ToolbarItems.AddCommandItem(new ToolbarCommandItem("RefreshImportBill",Resources.arrow_refresh, "刷新列表", true, true, true,
                                                               RefreshCallback));

            ToolbarItems.AddCommandItem(new ToolbarCommandItem("Close", Resources.cancel, "关闭", true, true, true,
                                                               CloseCallback));
        }

        private void PrintCategoryBookCallback(object sender, EventArgs e)
        {
            ImportBillBase importBill = bindingSource1.Current as ImportBillBase;
            if (importBill == null)
            {
                Functions.ShowMessageBox("请选择要打印的进口单");
                return;
            }
            IList<CategoryBookItem> bookitems = new List<CategoryBookItem>();
            ImportBill import = Operation.TradeSystem.GetImportBillDetail(importBill.PrimaryKey);
            if (import.BillItems != null)
            {
                foreach (ImportBillItem item in import.BillItems)
                {

                    CompanyProductView view = new CompanyProductView();
                    ProductPrice pp = new ProductPrice();
                    pp.Price = item.Price;
                    pp.CompanyStore = item.MaptoExportBillItem.Product;
                    pp.ShortCode = item.MaptoExportBillItem.ShortCode;

                    view.OnCompany = GlobalInfo.CurrentCompany;
                    view.PackageCount = item.ExportItem_PackageCount;
                    view.PackageUnitCount = item.PackageCount;
                    view.Price = pp;
                    view.Store = item.MaptoExportBillItem.Product;
                    view.TotalBulk = item.MaptoExportBillItem.TotalBulk;
                    view.TotalWeight = item.MaptoExportBillItem.TotalWeight;

                    CategoryBookItem books = new CategoryBookItem();
                    books.OrderKey = 0;
                    books.ProductView = view;
                    bookitems.Add(books);
                }
            }

            //if (currentObj == null) return;
            if (headimage == null || bodyimage == null)
                TemplateSettingCallBack(null, null);
            if (headimage == null) headimage = Properties.Resources.lifenghead;
            if (bodyimage == null) bodyimage = Properties.Resources.lifenglist;

            PrintProduct print = new PrintProduct();
            print.BodyImage = bodyimage;
            print.HeadImage = headimage;
            print.DataStorage = Operation.DataStorage;
            print.Exchange = importBill.ImportExchangeRate;
            print.PaintCaption = paintCaption;
            print.CaptionFont = captionFont;
            print.CaptionColor = captionColor;
            print.CompanyName = GlobalInfo.CurrentCompany.Caption;
            print.DrawchangdaProducts(bookitems, GlobalInfo.CurrentCompany);
        }

        private void TemplateSettingCallBack(object sender, EventArgs e)
        {
            CategoryBookPageSeting setting = new CategoryBookPageSeting();
            setting.ShowDialog();
            headimage = setting.HeadImage;
            bodyimage = setting.BodyImage;
            captionColor = setting.CaptionColor;
            captionFont = setting.CaptionFont;
            paintCaption = setting.PaintCaption;
            //if (!setting.PaintCaption) return;
            //Graphics g = Graphics.FromImage(headimage);
            //Brush b = new SolidBrush(setting.CaptionColor);
            //SizeF sf = PrintHelper.GetSizeF(GlobalInfo.CurrentCompany.Caption, setting.CaptionFont);
            //g.DrawString(GlobalInfo.CurrentCompany.Caption, setting.CaptionFont, b,
            //             new RectangleF((headimage.Width - sf.Width) / 2, (headimage.Height - sf.Height) / 2,sf.Width + 10, sf.Height));
        }

        private void PrintImportBillCallback(object sender, EventArgs e)
        {
            ImportBillBase importBill = bindingSource1.Current as ImportBillBase;
            if(importBill==null)
            {
                Functions.ShowMessageBox("请选择要打印的进口单");
                return;
            }

            PrintImport importPrinter = new PrintImport(importBill);
            importPrinter.ShowDialog();
        }

        private void ComfirmCounttingCallback(object sender, EventArgs e)
        {
            ImportBillBase billDetail = bindingSource1.Current as ImportBillBase;
            if (billDetail == null)
            {
                Functions.ShowMessageBox("请选择进口单");
                return;
            }
            ComfirmImportBillCountting comfirmForm = new ComfirmImportBillCountting(
                billDetail.ReceiveCompany.PrimaryKey, billDetail.CargoDate);
            if (comfirmForm.ShowDialog() != DialogResult.OK) return;
            Operation.TradeSystem.FinishImportBill(billDetail.PrimaryKey, comfirmForm.SelectDate,
                                                   comfirmForm.SelectEmployee.PrimaryKey);
            bindingSource1.RemoveCurrent();
        }

        private void BillDetailCallback(object sender, EventArgs e)
        {
            OpenCurentDetail();
        }

        private void RefreshCallback(object sender, EventArgs e)
        {
            ((ThreadStart)LoadDataSourceInvoke).BeginInvoke(null, null);            
        }

        private void CloseCallback(object sender, EventArgs e)
        {
            Close();
        }

        private void Import_Countting_Load(object sender, EventArgs e)
        {
            ((ThreadStart) LoadDataSourceInvoke).BeginInvoke(null, null);
        }

        private void LoadDataSourceInvoke()
        {
            IList<ImportBillBase> dataSource = new List<ImportBillBase>(
                    Operation.TradeSystem.GetCounttingImportBillList(GlobalInfo.CurrentCompany.PrimaryKey)
                );
            bindingSource1.DataSource = dataSource;
        }

        private void OpenCurentDetail()
        {
            ImportBillBase billDetail = bindingSource1.Current as ImportBillBase;
            if(billDetail == null)
            {
                Functions.ShowMessageBox("请选择进口单");
                return;
            }
            ImportBillDetail detailForm = new ImportBillDetail(billDetail.PrimaryKey);
            detailForm.Show();
        }

        private void gridView1_KeyUp(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = false;
            if (e.KeyCode == Keys.Enter) OpenCurentDetail();
        }

        private void gridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button== MouseButtons.Left && e.Clicks>=2)
            {
                GridHitInfo hitInfo = gridView1.CalcHitInfo(e.Location);
                if (hitInfo.InRow) OpenCurentDetail();
            }
        }

        private void rptReceiveEmp_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            Employee emp = e.Value as Employee;
            if (emp != null) e.DisplayText = emp.EmpName;
        }
    }
}
